<template>
  <div>
    <el-form label-width="100px" :model="skuForm">
      <el-form-item label="SPU名称"> {{ spu.spuName }} </el-form-item>

      <el-form-item label="SKU名称">
        <el-input placeholder="SKU名称" v-model="skuForm.skuName"></el-input>
      </el-form-item>

      <el-form-item label="价格(元)">
        <el-input
          type="number"
          placeholder="价格(元)"
          v-model="skuForm.price"
        ></el-input>
      </el-form-item>

      <el-form-item label="重量(千克)">
        <el-input
          type="number"
          placeholder="重量(千克)"
          v-model="skuForm.weight"
        ></el-input>
      </el-form-item>

      <el-form-item label="规格描述">
        <el-input
          type="textarea"
          rows="4"
          placeholder="规格描述"
          v-model="skuForm.skuDesc"
        ></el-input>
      </el-form-item>

      <!-- <el-form-item label="平台属性">
        <el-form label-width="80px">
          <el-form-item label="摄像头像">
            <el-select v-model="model" placeholder="请选择">
              <el-option label="label" value="value"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item> -->

      <el-form-item label="销售属性">
        <el-form :inline="true" label-width="100px">
          <el-form-item
            :label="spuSaleAttr.saleAttrName"
            v-for="spuSaleAttr in spuSaleAttrList"
            :key="spuSaleAttr.id"
          >
            <el-select
              v-model="spuSaleAttr.spuSaleAttrValueId"
              placeholder="请选择"
            >
              <el-option
                :label="spuSaleAttrValue.saleAttrValueName"
                :value="spuSaleAttrValue.id"
                v-for="spuSaleAttrValue in spuSaleAttr.spuSaleAttrValueList"
                :key="spuSaleAttrValue.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>

      <el-form-item label="图片列表">
        <el-table border style="width: 100%" :data="spuImageList">
          <el-table-column type="selection" width="55"> </el-table-column>

          <el-table-column prop="prop" label="图片" width="width">
            <template slot-scope="{ row }">
              <img
                :src="row.imgUrl"
                alt=""
                style="width: 100px; height: 100px"
              />
            </template>
          </el-table-column>

          <el-table-column prop="imgName" label="名称" width="width">
          </el-table-column>

          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row }">
              <el-button
                v-if="row.isDefault === '0'"
                type="primary"
                size="mini"
                @click="changeDefault(row, spuImageList)"
                >设为默认</el-button
              >
              <el-tag v-else type="info">默认</el-tag>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="save">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "SkuForm",
  data() {
    return {
      spu: {},
      spuSaleAttrList: [],
      skuImageList: [],
      spuImageList: [],
      skuForm: {
        spuId: "",
        tmId: "",
        category3Id: "",

        // v-model 直接收集
        price: "",
        skuName: "",
        skuDesc: "",
        weight: "",

        skuDefaultImg: "",

        skuImageList: [],
        // skuAttrValueList: [],
        // skuSaleAttrValueList: [],
      },
    };
  },
  methods: {
    async getInitAddSkuFormData(row) {
      this.spu = row;
      // 获取spu的销售属性值列表
      let promise1 = await this.$API.sku.getSpuSaleAttrList(row.id);
      // 获取spu的图片列表
      let promise2 = await this.$API.sku.getSpuImageList(row.id);
      const result = await Promise.all([promise1, promise2]);
      this.spuSaleAttrList = result[0].data;
      let spuImageList = result[1].data;
      spuImageList.forEach((item) => (item.isDefault = "0"));
      this.spuImageList = spuImageList;
    },

    // 设为默认
    changeDefault(row, spuImageList) {
      spuImageList.forEach((item) => (item.isDefault = "0"));
      row.isDefault = "1";
      this.skuForm.skuDefaultImg = row.imgUrl;
    },

    // 清空data中的数据
    resetData(){
       Object.assign(this._data,this.$options.data())
    },

    // 保存
    async save() {
      let { spu, skuForm, skuImageList } = this;
      skuForm.spuId = spu.id;
      skuForm.tmId = spu.tmId;
      skuForm.category3Id = spu.category3Id;

      // 整理skuImageList
      // {
      //   imgName: "string",
      //   imgUrl: "string",
      //   isDefault: "string",
      //   spuImgId: 0,
      // },
      skuForm.skuImageList = skuImageList.map((item) => {
        return {
          imgName: item.imgName,
          imgUrl: item.imgUrl,
          isDefault: item.isDefault,
          spuImgId: item.id,
        };
      });

      // 发请求
      try {
        const result = await this.$API.sku.addUpdate(skuForm);
        if (result.code === 20000 || result.code === 200) {
          this.$message.success("保存Sku成功");
          this.$emit('update:visible',false)
          this.resetData()
        }else{
          this.$message.error("保存Sku失败")
        }
      } catch (error) {
        this.$message.error("请求Sku失败")
      }
    },

    // 取消操作
    cancel() {
      this.$emit("update:visible", false);
      this.resetData()
    },
  },
};
</script>

<style lang="less" scoped>
</style>
